//
//  ChangePasswordIndexViewController.m
//  ELife
//
//  Created by  刘奇男 on 11-5-20.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ChangePasswordIndexViewController.h"
#import "M13UserDefaults.h"
#import "GDataXMLNode.h"
#import "ELifeAppDelegate.h"
#import "GTMBase64.h"
#import "AlertView.h"
#import "GetMyData.h"
#import "ModifyDeliveryInfoViewController.h"
#import "LoginController.h"


@implementation ChangePasswordIndexViewController

@synthesize confirmPassword, oldPassword, newPassword;


- (void)request
{  
    NSString* userid = [GetMyData getUserId];
    if (userid == nil)
    {
        LoginController* lc = [[LoginController alloc] init:self];
        [self.navigationController pushViewController:lc animated:YES];
        [lc release];
        return;
        
    }
    
    //1.6 修改密码
    //请求地址: /user.php
    //请求参数: action => updatepassword
    //user_id => 用户id
    //old_password	=> 旧密码
    //new_password	=> 新密码
    //返回参数:
    //
    //返回格式事例:
    //<?xml version="1.0" encoding="UTF-8"?><msg><head><returncode>0</returncode><transcode>updatebaseinfo</transcode></head></msg>
    
    NSString* host =  [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil];
    
    NSURL* url = [[NSURL alloc] initWithString:[host stringByAppendingString:@"user.php"]];
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:3];
    
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    [param setObject:[GetMyData getUserId] forKey:@"user_id"];
    [param setObject:@"updatepassword" forKey:@"action"];
    
    
    [param setObject:self.oldPassword.text forKey:@"old_password"];
    [param setObject:self.confirmPassword.text forKey:@"new_password"];
    
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"iRequestFinished:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[[ELifeAppDelegate App] httpRequestPoint]openUrl:dic postParam:param target:self];    [[ELifeAppDelegate App] showProgressDialog:@"请稍候"];
}

- (void)iRequestFinished:(NSData *)response
{
    //    返回格式事例: <?xml version="1.0" encoding="UTF-8"?><msg><head><returncode>0</returncode><transcode>addmailingaddress</transcode></head><body><mail_id>76</mail_id></body></msg>
    
    [[ELifeAppDelegate App] hideProgressDialog];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[GTMBase64 decodeData:response] options:0 error:nil];
    
    GDataXMLElement* rootElement = [doc rootElement];    
    NSLog(@"rootElement:------%@\n", rootElement);
    
    GDataXMLElement* headsElement = [[rootElement elementsForName:@"head"]objectAtIndex:0];
    
    NSString* transcode = [[[headsElement elementsForName:@"transcode"] objectAtIndex:0] stringValue];
    
    
    GDataXMLNode* returnCode = [[headsElement elementsForName:@"returncode"] objectAtIndex:0];
    
    [doc release];
    
    NSInteger code = [[returnCode stringValue] intValue];
    if (code == -105){
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录"
                                                       delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
        [alert show];
        [alert setTag:1];
        [alert release];
        return;
    } else if (code != 0){
        [AlertView showAlert:@"修改失败"];
        [doc release];
        return;
    }
    
    [M13UserDefaults setUserDefaults:[NSArray arrayWithObjects:[GetMyData getUserName], confirmPassword.text, [GetMyData getUserId], nil] defaultName:USERINFO_NAMEANDPASSWORD];
    
    [AlertView showAlert:@"修改成功"];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    
    
    [confirmPassword release];
    [oldPassword release];
    [newPassword release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [confirmPassword setDelegate:self];
    [oldPassword setDelegate:self];
    [newPassword setDelegate:self];
    
    [self.navigationItem setTitle:@"修改密码"]; 
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)confirm:(id)sender
{
//    [self.navigationController popViewControllerAnimated:YES];
    if ([confirmPassword.text length] == 0
        || [newPassword.text length] == 0
        || [oldPassword.text length] == 0)
    {
        [AlertView showAlert:@"请确认输入的密码"];
        return;
    }
    if ( ![confirmPassword.text isEqualToString:newPassword.text])
    {
        [AlertView showAlert:@"两次输入的密码不一致"];
        return;
    }
    
    [self request];
}

-(IBAction)cancel:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	// the user pressed the "Done" button, so dismiss the keyboard
	[textField resignFirstResponder];
	return YES;
}

- (IBAction)closeKeypad:(id)sender
{
    [confirmPassword resignFirstResponder];
    [oldPassword resignFirstResponder];
    [newPassword resignFirstResponder];
    
    [sender resignFirstResponder];
}

@end
